package systems.rpgBuilder;

import java.util.LinkedList;
import java.util.List;

import org.jdom.Element;

import systems.languages.MultiLanguages;

/**
 * Faction class is use to create a typical RPG faction according to the data
 * given by the RpgBuilder configuration file.
 * Typically, a Faction contains :
 * <ul>
 *  <li>The multilanguage property key linked to this faction.</li>
 *  <li>The table name where to get faction's data</li>
 *  <li>A picture (here it's the path to the picture)</li>
 *  <li>Unique identifier</li>
 * </ul>
 * @see Take a look a the RpgBuilder.dtd (not include in this Javadoc) to get
 * more details about the differents configurations allowed to create a 
 * faction. 
 * @author Baptiste Legrand & Benjamin Orman
 * @version 1.0
 */
public class Faction {
  //The multilanguage property key linked to this faction.
  private String multiLangueName;
  //The table name where to get the data about this faction.
  private String tableName;
  //The path to the picture of this faction.
  private String picturePath;
  //The unique identifier of this faction.
  private String id;

  /**
   * This constructor creates a Faction according to an XML Element given in
   * parameter.
   * @see Take a look a the RpgBuilder.dtd (not include in this Javadoc) to get
   * more details about the differents configurations allowed to create a 
   * faction. 
   * @param faction
   */
  public Faction(Element faction)
  {
    this.multiLangueName = faction.getChildText("multiLangueName");
    this.tableName = faction.getChildText("tableName");
    this.picturePath = faction.getChildText("imagePath");
    this.id = faction.getAttributeValue("id");
  }

  /**
   * Returns the multiLanguage property key which coulb be use to get the 
   * faction's name according to the current language loaded.
   * @return the multiLangueName - The multilanguage property key linked to 
   * this faction
   */
  public String getMultiLanguageName() {
    return multiLangueName;
  }

  /**
   * Returns the name of the table where to get the data about this faction.
   * @return the tableName - The name of the table where to get data about this
   * faction.
   */
  public String getTableName() {
    return tableName;
  }

  /**
   * Returns the path to the picture of this Faction
   * @return the picturePath - The path to the picture of this Faction
   */
  public String getPicturePath() {
    return picturePath;
  }

  /**
   * Returns the unique identifier of this faction.
   * @return String - The unique identifier of a faction
   */
  public String getId()
  {
    return this.id;
  }

  /**
   * Returns the Faction's name according to the current language used 
   * currently use by the program.
   * @return String - Faction's name.
   */
  public String toString()
  {
    return MultiLanguages.getProperties(multiLangueName);
  }

  /**
   * Returns a list of the authorized Classe of this Faction
   */
  public List<Classe> authorizedClasse()
  {
    return authorizedClasse(RpgBuilder.getClassesList());
  }

  /**
   * Returns a list of the authorized Classe containing  of this Faction 
   * @param classList - 
   * @return 
   */
  private List<Classe> authorizedClasse(List<Classe> classList)
  {
    List<Classe> authorizedClasse = new LinkedList<Classe>();
    for(Classe classe : classList)
    {
      if(classe.factionAuthorized(this))
      {
        authorizedClasse.add(classe);
        if(classe.getSubClassesList()!= null)
          authorizedClasse.addAll(authorizedClasse(classe.getSubClassesList()));
      }
    }
    return authorizedClasse;
  }
}
